package com.android.customviewgroup.viewholder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewStub;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;

/**
 * Created by 小神有礼了
 * Time: 2/20/22 1:43 AM
 * Mail: 5267621@qq.com
 */
public class ViewHolder implements IViewHolder {

    private View mConvertView;

    private SparseArray<View> mViews;

    private Context mContext;

    private ViewHolder(View convertView) {
        this.mViews = new SparseArray<View>();
        this.mConvertView = convertView;
        mContext = mConvertView.getContext();
    }

    public static IViewHolder create(View convertView) {
        return new ViewHolder(convertView);
    }

    @Override
    public View getConvertView() {
        return mConvertView;
    }


    /**
     * @param viewId view activity_id
     * @return view
     */
    @Override
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    @Override
    public CharSequence getText(@IdRes int viewId) {
        TextView textView = getView(viewId);
        return textView.getText();
    }

    @Override
    public int getVisibility(@IdRes int viewId) {
        return getView(viewId).getVisibility();
    }

    /**
     * @param viewId     view activity_id
     * @param visibility view display state
     * @return view holder
     */
    @Override
    public IViewHolder setVisibility(int viewId, int visibility) {
        View view = getView(viewId);
        if (view != null && view.getVisibility() != visibility) {
            view.setVisibility(visibility);
        }
        return this;
    }

    @Override
    public IViewHolder setEnabled(@IdRes int viewId, boolean enabled) {
        View view = getView(viewId);
        if (view != null) {
            view.setEnabled(enabled);
        }
        return this;
    }

    @Override
    public IViewHolder setCheckBox(@IdRes int viewId, boolean checked) {
        View view = getView(viewId);
        if (view != null && view instanceof CheckBox) {
            ((CheckBox) view).setChecked(checked);
        }
        return this;
    }

    @Override
    public IViewHolder setText(@IdRes int viewId, @StringRes int resId) {
        return setText(viewId, mContext.getResources().getString(resId));
    }

    @Override
    public IViewHolder setText(@IdRes int viewId, CharSequence text) {
        TextView view = getView(viewId);
        if (view != null && text != null) {
            view.setText(text);
        }
        return this;
    }

    @Override
    public IViewHolder setTextColor(int viewId, @ColorInt int textColor) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(textColor);
        }
        return this;
    }

    @Override
    public IViewHolder setTextColorRes(int viewId, @ColorRes int resId) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(mContext.getResources().getColor(resId));
        }
        return this;
    }

    @Override
    public IViewHolder setHint(@IdRes int viewId, @StringRes int resId) {
        return setHint(viewId, mContext.getString(resId));
    }

    @Override
    public IViewHolder setHint(@IdRes int viewId, CharSequence text) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setHint(text);
        }
        return this;
    }

    @Override
    public IViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnClickListener(int viewId, Object tag, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setTag(tag);
            view.setOnClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnLongClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnLongClickListener(int viewId, Object tag,
                                              View.OnLongClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setTag(tag);
            view.setOnLongClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnTouchListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder addTextChangedListener(int viewId, TextWatcher listener) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).addTextChangedListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setBackgroundResource(@IdRes int viewId, @DrawableRes int drawable) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundResource(drawable);
        }
        return this;
    }

    @Override
    public IViewHolder setBackgroundColor(int viewId, int colorId) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundColor(mContext.getResources().getColor(colorId));
        }
        return this;
    }

    @Override
    public IViewHolder setImageResource(@IdRes int viewId, @DrawableRes int drawableId) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageResource(drawableId);
        }
        return this;
    }

    @Override
    public IViewHolder setImageResource(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageDrawable(drawable);
        }
        return this;
    }

    @Override
    public IViewHolder setImageResource(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageBitmap(bitmap);
        }
        return this;
    }

    @Override
    public IViewHolder setImageURI(int viewId, Uri uri) {
        View view = getView(viewId);
        if (view != null && view instanceof ImageView) {
            ((ImageView) view).setImageURI(uri);
        }
        return this;
    }

    @Override
    public IViewHolder setColorFilter(int viewId, int resId) {
        View view = getView(viewId);
        if (view != null && view instanceof ImageView) {
            ((ImageView) view).setColorFilter(resId);
        }
        return this;
    }

    @Override
    public IViewHolder inflate(int viewId) {
        View view = getView(viewId);
        if (view != null && view instanceof ViewStub) {
            try {
                ((ViewStub) view).inflate();
            } catch (Exception e) {
                //inflate只能执行1次
                view.setVisibility(View.VISIBLE);
            }
        }
        return this;
    }

    @Override
    public IViewHolder recycle() {
        mViews.clear();
        mViews = null;
        mConvertView = null;
        return this;
    }
}
